//
//  IGDatePickerView.m
//  TimeTest
//
//  Created by 王鹏宇 on 2018/1/31.
//  Copyright © 2018年 LvJianfeng. All rights reserved.
//

#import "IGDatePickerView.h"

@interface IGDatePickerView()

@property (nonatomic, strong)UIDatePicker * datePicker;

@end
@implementation IGDatePickerView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    
 
    self.backgroundColor = [UIColor whiteColor];
    
    self.frame = CGRectMake(0,0,kScreenWidth,245);
    if (ISiPhoneX) {
        self.frame = CGRectMake(0,0,kScreenWidth,279);
    }
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0,24,kScreenWidth, 216)];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePicker.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.datePicker];
    
    //[self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];//重点：UIControlEventValueChanged
    //设置显示格式
    //默认根据手机本地设置显示为中文还是其他语言
    
    NSString * str = @"zh_CN";
    if ([[AppManager sharedManager].lanstr containsString:@"en"]){
        str = @"en_US";
    }
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:str];//设置为中文显示
    self.datePicker.locale = locale;
    
    //当前时间创建NSDate
    NSDate *localDate = [NSDate date];
    //设置最大值时间
    
    self.datePicker.maximumDate = localDate;
    if (self.selectedDate) {
        
        [self.datePicker setDate:self.selectedDate animated:NO];
    }
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(0, 5, 90, 24);
    [self addSubview:cancelBtn];
    [cancelBtn setTitle:MYLocalizedString(@"PUBLIC_CANCEL", nil) forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setTitleColor:kThemeColor forState:UIControlStateNormal];
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectMake(self.frame.size.width - 90, 5, 90, 24);
    [self addSubview:confirmBtn];
    [confirmBtn setTitle:MYLocalizedString(@"PUBLIC_CONFIRM", nil) forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [confirmBtn setTitleColor:kThemeColor forState:UIControlStateNormal];
    
    
}

-(void)dateChanged:(id)sender{
    
    //NSDate格式转换为NSString格式
    self.selectedDate = [self.datePicker date];// 获取用户通过UIDatePicker设置的日期和时间

}

- (void)cancelBtnClicked:(UIButton *)sender {
    
    if (_delegate && [_delegate respondsToSelector:@selector(cancelSelet)]) {
        [_delegate cancelSelet];
    }
}

- (void)confirmBtnClicked:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(confirmTheDate:)]) {
        self.selectedDate = [self.datePicker date];
        [_delegate confirmTheDate:self.selectedDate];
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
